package com.jn.news.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.news.R;
import com.jn.news.adapter.TianxingNewsAdapter;
import com.jn.news.model.NewsDto;
import com.jn.news.utils.Constant;
import com.jn.news.utils.StatusBarUtils;
import com.jn.news.utils.ToastUtil;
import com.jn.news.viewmodel.TianxingNewsListViewModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class TianxingNewsListActivity extends AppCompatActivity {
    private SmartRefreshLayout smartRefreshLayout;
    private Integer col;
    private Integer pageNum = 1;
    private Integer pageSize = 10;
    private List<NewsDto> newsList = new ArrayList<>();
    private RecyclerView mrv;
    private TianxingNewsAdapter newsAdapter;
    private TianxingNewsListViewModel viewModel;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.with(this).init3(2, 102);
        setContentView(R.layout.activity_tianxing_news_list);
        viewModel = new ViewModelProvider(this).get(TianxingNewsListViewModel.class);
        initView();
        initData();

    }

    private void initView() {
        smartRefreshLayout = findViewById(R.id.smartRefreshLayout);
        mrv = findViewById(R.id.mRv);
        tvTitle = findViewById(R.id.toolbar_title);
        ImageView ivLeft = findViewById(R.id.toolbar_left);
        ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        col = getIntent().getIntExtra(Constant.NEWS_TYPE, 0);
        String newsTypeName = getIntent().getStringExtra(Constant.NEWS_TYPE_NAME);
        tvTitle.setText(newsTypeName);
        mrv.setLayoutManager(new LinearLayoutManager(this));
        newsAdapter = new TianxingNewsAdapter(this);
        mrv.setAdapter(newsAdapter);
        getNewsData();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNum++;


                getNewsData();

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 1;
                getNewsData();

            }
        });
    }

    private void getNewsData() {
        viewModel.getNewsList(col, pageNum, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                            if (response.getCode() == 200) {
                                if (pageNum == 1) {
                                    newsList.clear();
                                    newsList.addAll(response.getResult().getNewslist());
                                    Log.e("----------", "getNewsData: ----"+response.getResult().getNewslist().get(0).getPicUrl() );
                                } else {
                                    newsList.addAll(response.getResult().getNewslist());
                                }
                                newsAdapter.addAll(newsList);
//                                newsAdapter.notifyDataSetChanged();
                                smartRefreshLayout.finishRefresh();
                                smartRefreshLayout.finishLoadmore();
                            } else {
                                smartRefreshLayout.finishRefresh();
                                smartRefreshLayout.finishLoadmore();
                                ToastUtil.showToast(this, "请求失败: " + response.getMessage());
                            }
                        }
                        , throwable -> {
                            ToastUtil.showToast(this, "网络错误");
                        });

    }
}